<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('member.invoice.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="150px" ref="formRef" :model="dataForm" :rules="dataRule">

          <el-form-item :label="i18n('member.invoice.nickname')">
            <el-text v-if="dataForm.member">
              {{ dataForm.member.chinese_nickname }}
            </el-text>
          </el-form-item>

          <el-form-item :label="i18n('member.invoice.category')">
            <el-text>
              {{ dataForm.category }}
            </el-text>
          </el-form-item>

          <el-form-item :label="i18n('member.invoice.type')">
            <el-text>
              {{ dataForm.type }}
            </el-text>
          </el-form-item>

          <el-form-item :label="i18n('member.invoice.code')">
            <el-text>
              {{ dataForm.code }}
            </el-text>
          </el-form-item>

          <el-form-item :label="i18n('member.invoice.name')">
            <el-text>
              {{ dataForm.name }}
            </el-text>
          </el-form-item>

          <el-form-item :label="i18n('member.invoice.email')">
            <el-text>
              {{ dataForm.email }}
            </el-text>
          </el-form-item>

          <el-form-item :label="i18n('member.invoice.mobile')">
            <el-text>
              {{ dataForm.mobile }}
            </el-text>
          </el-form-item>

          <el-form-item :label="i18n('common.audit_status')" prop="audit_status">
            <el-select v-model="dataForm.audit_status" :placeholder="$t('common.please_select') + $t('common.audit_status')" clearable>
              <el-option v-for="(v, k) in auditStatusList" :label="v.text" :key="k" :value="v.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('module:member:invoice:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="member-invoice-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('member/invoice')

  const auditStatusList = ref([])

  const dataForm = reactive({
    id: 0,
    member: '',
    category: '',
    type: '',
    code: '',
    name: '',
    email: '',
    mobile: '',
    audit_status: '1',
  })

  const dataRule = reactive({
    audit_status: [{
      required: true,
      message: i18n('member.invoice.rules.audit_status.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/member/invoice/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.member = data.data.member
            dataForm.category = data.data.category.text
            dataForm.type = data.data.type.text
            dataForm.code = data.data.code
            dataForm.name = data.data.name
            dataForm.email = data.data.email
            dataForm.mobile = data.data.mobile
          }
        })
      }
    })
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2024-05-30
   *
   * 获取审核状态信息
   *
   * @return {[type]}
   */
  const loadAuditStatusList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'audit_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        auditStatusList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadAuditStatusList()
  })
</script>
